//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

/****************************************************************************
    Micron MT28F128J3
****************************************************************************/
#define     TOTAL_BLOCKS            128
#define     BLOCK_MASK              0xFFFFE000
#define     FLASH_SIZE              0x01000000          /* 16MB */

#define     FACTORY_CODE_OFFSET     0x0000
#define     USER_CODE_OFFSET        0x0020
#define     PROTECTION_LOCK_OFFSET  0x0040

#define     VENDOR_CODE_OFFSET      0x00
#define     DEVICE_CODE_OFFSET      0x01



/****************************************************************************
    Flash Command Definitions
****************************************************************************/
#define FLASH_CMD_ERASE_SETUP           0x00200020
#define FLASH_CMD_WRITE_SETUP           0x00400040
#define FLASH_CMD_CLEAR                 0x00500050
#define FLASH_CMD_LOCK_SETUP            0x00600060
#define FLASH_CMD_STATUS                0x00700070
#define FLASH_CMD_READ_ID               0x00900090
#define FLASH_CMD_SUSPEND               0x00B000B0
#define FLASH_CMD_PROGRAM_PROTECTION    0x00c000c0
#define FLASH_CMD_RESUME                0x00D000D0
#define FLASH_CMD_CONFIRM               0x00D000D0
#define FLASH_CMD_READ                  0x00FF00FF

#define FLASH_CMD_LOCK                  0x00010001
#define FLASH_CMD_UNLOCK                0x00d000d0
#define FLASH_CMD_LOCK_ALL              0x00FF00FF
#define FLASH_CMD_UNLOCK_ALL            0x00000000
#define FLASH_CMD_LOCK_PROTECTION       0x7FFF7FFF


/****************************************************************************
    Status Register Definitions
****************************************************************************/
#define SR_READY                0x00800080          /* bit 7 set */
#define SR_ERASE_SUSPEND        0x00400040          /* bit 6 set */
#define SR_ERASE_ERR            0x00200020          /* bit 5 set */
#define SR_WRITE_ERR            0x00100010          /* bit 4 set */
#define SR_VPP_ERR              0x00080008          /* bit 3 set */
#define SR_WRITE_SUSPEND        0x00040004          /* bit 2 set */
#define SR_LOCK                 0x00020002          /* bit 1 set */

#define STATUS_READY(x)         ((x) == SR_READY)               /* ony bit 7 set */
#define STATUS_BUSY(x)          (((x) & SR_READY) != SR_READY)  /* bit 7 clear */
#define STATUS_IDLE(x)          (((x) & SR_READY) == SR_READY)  /* bit 7 set, don't care other bits */
#define STATUS_ERASE_SUSPEND(x) (STATUS_IDLE(x) && ((x) & SR_ERASE_SUSPEND))    /* bit 7 and bit 6 set */
#define STATUS_WRITE_SUSPEND(x) (STATUS_IDLE(x) && ((x) & SR_WRITE_SUSPEND))    /* bit 7 and bit 2 set */
#define STATUS_LOCK(x)          (STATUS_IDLE(x) && ((x) & SR_LOCK))             /* but 7 and bit 1 set */
#define STATUS_ERROR(x)         (STATUS_IDLE(x) && ((x) & (SR_WRITE_ERR | SR_ERASE_ERR | SR_LOCK | SR_VPP_ERR)))



/****************************************************************************
    Block Locking Mode Definitions
****************************************************************************/
#define MODE_UNLOCK             0x00000000
#define MODE_LOCK               0x00010001
#define MODE_LOCK_DOWN          0x00020002

/****************************************************************************
    Return Codes
****************************************************************************/
#define RET_OK              0       /* operation success */
#define RET_STATUS_ERROR    1       /* operation fail */

/****************************************************************************
    Function Prototypes
****************************************************************************/
void    MTFlashClearStatus(ulong_t);
void    MTFlashSetReadArray(ulong_t);
ulong_t MTFlashReadStatus(ulong_t);
ulong_t FlashDeviceStatus(void);
int     MTFlashBlockErase(ulong_t);
int     MTFlashWrite(ulong_t, ulong_t);
int     FlashBlockLockStatus(ulong_t, ushort_t*);
int     FlashBlockLock(ulong_t);
int     FlashBlockUnlock(ulong_t);
int     FlashBlockLockAll(void);
int     FlashBlockUnlockAll(void);
int     FlashGetDeviceId(ushort_t *);
int     FlashGetVendorId(ushort_t *);
int     FlashReadFactoryCode(uchar_t *);
int     FlashReadUserCode(uchar_t *);
int     FlashProgramUserCode(uchar_t *);
int     FlashReadUserCodeLockStatus(ushort_t *);
int     FlashLockUserCode(void);

EXTERN_C int     MTFlashEraseSector(ulong_t ulFlashBase, long lSectorOffset);
EXTERN_C int     MTFlashWriteBuffer (ulong_t FlashBase, long Offset, uchar_t *Buffer, long BufferSize);
EXTERN_C int     MTFlashReadBuffer  (ulong_t FlashBase, long Offset, uchar_t *Buffer, long BufferSize);

#define FlashEraseSector    MTFlashEraseSector
#define FlashWriteBuffer    MTFlashWriteBuffer
#define FlashReadBuffer     MTFlashReadBuffer

